package sunwin.yoms.dao.comm;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

/**
 * Created by 田西泽 on 2015-8-27.
 */
public class MybatisHelper {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            ClassPathResource resource = new ClassPathResource("/sunwin.yoms.properties");
            Properties props = PropertiesLoaderUtils.loadProperties(resource);

            //创建SqlSessionFactory
            Reader reader = Resources.getResourceAsReader("/sunwin.yoms.log.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, props);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Session
     * @return
     */
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}

